package prepare_data

import (
	"anubis-framework/pkg/io"
	"gorm.io/gorm"
)

// 准备数据：21个交易日内涨跌最小的股票
type MinUpDownPercentage21PrepareData struct {
	db *gorm.DB
}

// 初始化
func (m *MinUpDownPercentage21PrepareData) Init(db *gorm.DB) {
	m.db = db
}

// 准备数据：清空quant_stock_filter表，从stock_transaction_data_all表中向quant_stock_filter表中插入记录，按照up_down_percentage_21升序排列，包括正在持有的股票
func (m *MinUpDownPercentage21PrepareData) DoPrepareData(currentDate string) {
	io.Infoln("准备数据：清空quant_stock_filter表，从stock_transaction_data_all表中向quant_stock_filter表中插入记录，按照up_down_percentage_21升序排列，包括正在持有的股票")

	// 清空quant_stock_filter表
	m.db.Exec("truncate table quant_stock_filter")

	// 从stock_transaction_data_all表中向quant_stock_filter表中插入记录，按照up_down_percentage_21升序排列，包括正在持有的股票
	m.db.Exec("insert /*+ append */ into quant_stock_filter(stock_code, up_down_percentage_21, total_market_value) "+
		"select distinct mtsd.code_, mtsd.up_down_percentage_21, stda.total_market_value "+
		"from mdl_top_stock_detail mtsd "+
		"join stock_transaction_data_all stda on stda.code_=mtsd.code_ and stda.date_=mtsd.date_ "+
		"where mtsd.date_=to_date(?, 'yyyy-mm-dd') and mtsd.up_down_percentage_21 is not null "+
		"order by mtsd.up_down_percentage_21 asc", currentDate)
}
